<!--
 * @Descripttion: 单据列表搜索组件
 * @version: V1.0
 * @Author: GW
 * @Date: 2021-08-25 08:28:27
 * @LastEditors: GW
 * @LastEditTime: 2022-01-14 17:13:36
-->

<template>
    <div class="dynamic-list-wrapper">
        <!-- <a-form-model v-if="false">
            <a-row class="order-search-list-box">
                <a-col :span="22">
                    <a-row>
                        <a-col :span="4" v-for="(item,index) in searchArray" :key="index">
                            <a-form-model-item v-if="item.columnType=='文本' && item.code != 'voucherState'" :label-col="labelCol" :wrapper-col="wrapperCol" :label="item.name">
                                <a-input v-model="item.value" allow-clear :placeholder="`请输入${item.name}`" />
                            </a-form-model-item>
                            <a-form-model-item v-if="item.columnType=='下拉选择'" :label-col="labelCol" :wrapper-col="wrapperCol" :label="item.name">
                                <a-select :value="item.value" style="width: 100%" @change="((a,b,c,d)=>{onChangeSelect(item.code,a)})">
                                    <template v-if="item.val != ''">
                                        <a-select-option v-for="select in formatSelectJsonVal(item.val)" :key="select.key">
                                            {{select.label}}
                                        </a-select-option>
                                    </template>
                                </a-select>
                            </a-form-model-item>
                            <a-form-model-item v-else-if="item.code == 'businessType'" :label-col="labelCol" :wrapper-col="wrapperCol" label="业务类型">
                                <a-select v-model="item.value" allow-clear>
                                    <a-select-option v-for="(select,index) in formatSelectJsonVal(item.val)" :key="index +'select2'" :value="select.key"> {{select.label}}</a-select-option>
                                 <a-select-option :value="1">普通采购</a-select-option>
                                    <a-select-option :value="2">固定资产</a-select-option>
                                    <a-select-option :value="3">普通销售</a-select-option>
                                    <a-select-option :value="4">领料</a-select-option>
                                    <a-select-option :value="5">退料</a-select-option>
                                    <a-select-option :value="6">补料</a-select-option>
                                    <a-select-option :value="9">生产完工</a-select-option>
                                    <a-select-option :value="10">自制生产</a-select-option>
                                    <a-select-option :value="11">委外加工</a-select-option>
                                    <a-select-option :value="12">其他</a-select-option>
                                    <a-select-option :value="13">正常加工</a-select-option>
                                    <a-select-option :value="14">整单委外</a-select-option>
                                    <a-select-option :value="15">工序委外</a-select-option>
                                    <a-select-option :value="30">委外到货</a-select-option>
                                    <a-select-option :value="31">委外退货(冲抵)</a-select-option>
                                    <a-select-option :value="32">委外退货</a-select-option>
                                    <a-select-option :value="96">销售退货(冲抵)</a-select-option>
                                    <a-select-option :value="97">采购退货(冲抵)</a-select-option>
                                    <a-select-option :value="98">销售退货</a-select-option>
                                    <a-select-option :value="99">采购退货</a-select-option> 
                                </a-select>
                            </a-form-model-item>
                            <a-form-model-item v-else-if="item.code == 'workType'" :label-col="labelCol" :wrapper-col="wrapperCol" label="加工类型">
                                <a-select v-model="item.value" allow-clear>
                                    <a-select-option :value="1">加工</a-select-option>
                                    <a-select-option :value="2">返工</a-select-option>
                                </a-select>
                            </a-form-model-item>
                            <a-form-model-item v-else-if="item.code == 'voucherType'" :label-col="labelCol" :wrapper-col="wrapperCol" label="单据类型">
                                <a-select v-model="item.value" allow-clear>
                                    <a-select-option :value="1">普通</a-select-option>
                                    <a-select-option :value="2">倒冲</a-select-option>
                                </a-select>
                            </a-form-model-item>

                            <a-form-model-item v-else-if="item.columnType == '日期'" :label-col="labelCol" :wrapper-col="wrapperCol" :label="`请选择${item.name}`">
                                <a-date-picker v-model="item.value" :disabled="!item.onEdit" @change="((date,dateString)=>{onChangeSelect(date,dateString,item.code)})" style="min-width:150px; width:auto;" format="YYYY-MM-DD" />
                            </a-form-model-item>

                            <a-form-model-item v-else-if="item.columnType == '搜索选择'" :label-col="labelCol" :wrapper-col="wrapperCol" :label="item.name">
                                <a-input read-only v-model="item.showvalue" :placeholder="`请选择${item.name}`" @click="searchGroup(item.code+'Ref')"></a-input>
                                <form-search-group :ref="item.code+'Ref'" :dynamicData="item" @clickItem="searchCheckData"></form-search-group>
                            </a-form-model-item>

                        </a-col>
                    </a-row>
                </a-col>
                <a-col :span="2" style="text-align:right;padding-right:10px;">
                    <a-button type="primary" icon="search" @click="searchList">查询</a-button>
                </a-col>
            </a-row>
        </a-form-model> -->

        <a-row :gutter="[16,8]">
            <a-col :span="24">
                <div class="search-btn-box">
                    <div class="item-small">
                        <ul>
                            <li class="item-plan" :class="planActive == 0?'active':''" @click="searchList('all')">所有</li>
                            <li class="item-plan" :title="item.programmeName" :class="item.id == planActive?'active':''" v-for="(item,index) in myPlanList" :key="index" @click="changePlane(item)">
                                {{item.programmeName}}</li>
                            <li class="item-plan" @click="searchModelShow">
                                <!-- <a-icon class="icon-searh-btn" type="file-search" /> 查询 -->
                                自定查询
                                <a-icon class="icon-searh-btn" type="search" />
                            </li>
                        </ul>
                    </div>
                </div>
            </a-col>
        </a-row>
        <a-row :gutter="[0,16]">
            <a-col :span="24">
                <a-table id="dynamicList_tb" class="search-table-list" :columns="searchCloums" :scroll="{x:500,y:500}" :data-source="searchBackData" :customRow="customListClick" :row-selection="rowSelection" :rowClassName="rowClassName" bordered :pagination="pagination" @change="handleChange">
                    <slot slot="businessType" slot-scope="text,record">
                        {{handleBusinessType(record)}}
                    </slot>
                    <slot slot="voucherCode" slot-scope="text">
                        <!-- <a href="#" @click="onCopy(text)">{{text}}</a> -->
                        {{text}}
                    </slot>
                    <slot slot="voucherState" slot-scope="text,record">
                        <!-- -1:待提交 0：待审核 1：已审核 2：已确认 10：关闭 -->
                        <div v-if='text != 2'>
                            {{text==-1?'待提交':text==0?'待审核':text==1?'已审核':text==10?'关闭':''}}
                        </div>
                        <div v-else>
                            {{text==2 && record.receiveDeliverType == '1' ?'已入库':text==2 && record.receiveDeliverType == '2' ?'已出库':''}}
                        </div>
                    </slot>
                    <slot slot="workType" slot-scope="text">
                        <!-- 加工类型 -->
                        {{text==1?'加工':text==2?'返工':''}}
                    </slot>
                    <slot slot="voucherType" slot-scope="text">
                        <!-- 单据类型 -->
                        {{text==1?'普通':text==2?'倒冲':''}}
                    </slot>
                    <slot slot="planDirection" slot-scope="text">
                        <!-- 单据类型 -->
                        {{text==1?'收':text==2?'付':'其他'}}
                    </slot>
                    <!-- <div v-if="sourceCode == 'Contract'" slot="footer" slot-scope="text,record">
                        合同金额合计:{{record.contractPrice}}
                    </div> -->
                </a-table>
            </a-col>
        </a-row>

        <!--查询弹窗-->
        <a-modal class="search-wrapper-box" :visible="visible" title="查询" @cancel="cancel" width="1000px">
            <div class="content-box">
                <div class="left-box">
                    <a-row class="btn-wrapper">
                        <a-col :class="planActive != 0?'':'btns-disable'" :span="6">
                            <div class="btns" @click="planActive != 0?savePlan():''">
                                <a-icon class="btns-icon" type="save" />
                                <div class="btns-text">保存</div>
                            </div>
                        </a-col>
                        <a-col :span="6">
                            <div class="btns" @click="saveAsPlanShow(1)">
                                <a-icon class="btns-icon" type="save" />
                                <div class="btns-text">另存</div>
                            </div>
                        </a-col>
                        <a-col :class="planActive != 0?'':'btns-disable'" :span="6">
                            <div class="btns" @click="planActive != 0?saveAsPlanShow(2):''">
                                <a-icon class="btns-icon" type="edit" />
                                <div class="btns-text">修改</div>
                            </div>
                        </a-col>
                        <a-col :class="planActive != 0?'':'btns-disable'" :span="6">
                            <div class="btns" @click="planActive != 0?removePlan():''">
                                <a-icon class="btns-icon" type="delete" />
                                <div class="btns-text">删除</div>
                            </div>
                        </a-col>
                    </a-row>
                    <div class="plan-list">
                        <ul>
                            <li :class="planActive == 0?'active':''" @click="searchList('all')">默认方案</li>
                            <li v-for="(item,index) in myPlanList" :key="index" :class="item.id == planActive?'active':''" @click="changePlane(item)">{{item.programmeName}}</li>
                        </ul>
                    </div>
                </div>
                <div class="right-box">
                    <a-tabs :activeKey="activeKey" @change="callback">
                        <a-tab-pane key="1" tab="查询条件">
                            <a-row class="condition" v-if="syncBox" :gutter="[8,0]" style="margin-right:0px; height:385px;">
                                <div v-for="(item,index) in searchArray" :key="index">
                                    <a-col :span="12" v-if="item.columnType=='文本' && item.code != 'voucherState'">
                                        <div class="filter">
                                            <label class="filter-label">{{item.name}} :</label>
                                            <div class="filter-control">
                                                <a-input v-model="item.value" allow-clear size="small"></a-input>
                                            </div>
                                        </div>
                                    </a-col>

                                    <!-- <a-col :span="12" v-if="item.code == 'businessType'">
                                        <div class="filter">
                                            <label class="filter-label">业务类型 :</label>
                                            <div class="filter-control">
                                                <a-select v-model="item.value" allow-clear>
                                                    <a-select-option v-for="(select,index) in formatSelectJsonVal(item.val)" :key="index +'select2'" :value="select.key"> {{select.label}}</a-select-option>
                                                </a-select>
                                            </div>
                                        </div>
                                    </a-col> -->

                                    <a-col :span="12" v-if="item.columnType=='下拉选择'" style="height:42px;">
                                        <div class="filter">
                                            <label class="filter-label">{{item.name}} :</label>
                                            <div class="filter-control">
                                                <a-select :value="item.value" style="width: 100%" @change="((a,b,c,d)=>{onChangeSelect(item.code,a)})" allow-clear>
                                                    <template v-if="item.val != ''">
                                                        <a-select-option v-for="select in formatSelectJsonVal(item.val)" :key="select.key">
                                                            {{select.label}}
                                                        </a-select-option>
                                                    </template>
                                                </a-select>
                                            </div>
                                        </div>
                                    </a-col>

                                    <a-col :span="12" v-if="item.code == 'workType'">
                                        <div class="filter">
                                            <label class="filter-label">加工类型 :</label>
                                            <div class="filter-control">
                                                <a-select v-model="item.value" allow-clear>
                                                    <!--加工类型-->
                                                    <a-select-option :value="1">加工</a-select-option>
                                                    <a-select-option :value="2">返工</a-select-option>
                                                </a-select>
                                            </div>
                                        </div>
                                    </a-col>

                                    <a-col :span="12" v-if="item.code == 'voucherType'">
                                        <div class="filter">
                                            <label class="filter-label">单据类型 :</label>
                                            <div class="filter-control">
                                                <a-select v-model="item.value" allow-clear>
                                                    <!--单据类型-->
                                                    <a-select-option :value="1">普通</a-select-option>
                                                    <a-select-option :value="2">倒冲</a-select-option>
                                                </a-select>
                                            </div>
                                        </div>
                                    </a-col>
                                    <a-col :span="12" v-if="item.columnType == '日期'">
                                        <div class="filter">
                                            <label class="filter-label">{{item.name}} :</label>
                                            <div class="filter-control">
                                                <a-date-picker v-model="item.value" @change="((date,dateString)=>{onChangeSelect(date,dateString,item.code)})" style="min-width:150px; width:auto;" format="YYYY-MM-DD" />
                                            </div>
                                        </div>
                                    </a-col>
                                    <a-col :span="12" v-if="item.columnType == '搜索选择'">
                                        <div class="filter">
                                            <label class="filter-label">{{item.name}} :</label>
                                            <div class="filter-control">
                                                <div style="background:rgba(0,0,0,0);width:100%;height:28px;z-index:2;position:absolute;top:10px;left:0;" @click="searchGroup(item.code+'Ref')"></div>
                                                <a-input read-only v-model="item.showvalue" :placeholder="`请选择${item.name}`" @click="searchGroup(item.code+'Ref')"></a-input>
                                                <form-search-group :ref="item.code+'Ref'" :dynamicData="item" @clickItem="searchCheckData"></form-search-group>
                                            </div>
                                        </div>
                                    </a-col>
                                </div>
                            </a-row>
                        </a-tab-pane>

                    </a-tabs>
                </div>
            </div>
            <template slot="footer">
                <div>
                    <!-- <div style="float:left">
                    <a-checkbox :checked="planActive == defaultPlanActive?true:false" @change="inPagePlanSave">
                        下次以此方案进入
                    </a-checkbox>
                </div> -->
                    <a-button type="primary" @click="handlerSearch" v-if="planActive != 0">保存并查询</a-button>
                    <a-button type="primary" @click="searchList('close')">仅查询</a-button>
                    <a-button @click="cancel" class="margin-left-10">取消</a-button>
                </div>
            </template>
            <a-modal :title="nowPlanType==1?'保存为另一个方案':'修改方案标题'" :visible="saveASVisible" width="400px" centered @cancel="saveAsPlanClose">
                <a-input v-model="saveAsName" placeholder="请输入查询方案名称"></a-input>
                <template slot="footer">
                    <div>
                        <a-button type="primary" @click="saveAsPlanSubmit(1)">确定</a-button>
                        <a-button @click="saveAsPlanClose" class="margin-left-10">取消</a-button>
                    </div>
                </template>
            </a-modal>
        </a-modal>

    </div>
</template>
<script>


import { list as listDesign } from "@/api/erp/voucherDesign"; //动态表单数据
import { list as listSearch } from '@/api/purchase/voucherDesign';//搜索列表
import { IdFieldToNameField } from "@/utils/customize";
import VueClipboard from "vue-clipboard2";
Vue.use(VueClipboard);

import { Controlwidth } from "@/utils/antTableComponents";
import FormSearchGroup from '@/components/Form/FormSearchGroup';
import { myplan, systemplan, save, remove, detailsList } from '@/api/erp/reportProgramme';
import { conversionOrderName } from "@/utils/tool/parametric-switch"

export default {
    props: {
        sourceCode: {
            type: String
        },
        businessType: {
            type: Number
        },
        dataSourceCode: {//区分委外和不是委外的 领料单和加工单
            type: Number
        },
        basePostData: {
            type: Object,
            default: {
                someBusinessType: '0',    // 指定业务类型 默认 0
            }
        },
        excludeHeadOrFoodColumn: {
            type: Array,
            default: null
        },
        reportCode: {   //对应的列表搜索，保存方案code
            type: String,
        }
    },
    components: {
        FormSearchGroup
    },
    data () {
        return {
            labelCol: { span: 10 },
            wrapperCol: { span: 14 },
            mouseIndex: null,
            customListClick: (record, index) => ({
                on: {
                    mouseenter: (event) => {
                        event.preventDefault();
                        this.mouseIndex = index;
                        this.searchDetailsHover = record;
                        const detaileList = [];

                    },
                    dblclick: (event) => {
                        event.preventDefault();
                        //关闭侧边栏赋值给父组建
                        this.$emit('switchList', 'order');
                        this.$emit('dblclick', record.id);
                    }
                },
            }),
            pagination: {
                size: "small",
                current: 1,
                pageSize: 10,
                total: 0,
                showTotal: total => {
                    return (
                        <div>
                            共 {total} 条数据，每页
                            <a-input style="height:22px !important;width:3.2rem;"
                                value={this.pagination.pageSize}
                                size="small"
                                onPressEnter={e => this.changePageSize(e)}
                            />
                            条
                        </div>
                    );
                },
                onChange: (page, pageSize) => {
                    this.pagination.current = page;
                    this.pagination.pageSize = pageSize;
                    this.handleChange({
                        current: page,
                        pageSize: pageSize
                    });
                },
                // showQuickJumper: true,
                showLessItems: true,
                choiceBusinessType: null,//选单传入的业务类型
            },
            dynamicFormDataList: [],    //动态表单数据
            searchArray: [],        //搜索条件
            searchCloums: [],
            searchBackData: [],
            searchDetailsHover: [],  //鼠标移入当前数据的详细信息数据
            searchData: {},//搜索数组对象
            sorterData: {},//排序搜索对象
            selectedKeys: [],
            selectedRows: [],

            systemPlan: [],//系统方案
            activeKey: '1',
            visible: false,//查询弹窗
            planActive: 0,//当前选中的方案

            saveAsName: '',         //另存名称
            saveASVisible: false,   //另存名字弹窗
            myPlanList: [],         //个人自定义方案

            nowPlanData: [],        //当前显示数据
            nowPlanType: null,      //修改还是另存 //type==1新增，type==2改名
            syncBox: true,
        }
    },
    updated () {
        Controlwidth("dynamicList_tb", 120);
    },
    watch: {
        planActive () {
            this.syncBox = false;
            setTimeout(() => {
                this.syncBox = true;
            }, 200)
        },
        'pagination.current' (a, b) {
            console.log('pagination.current:', a, b)
        }
    },
    computed: {
        rowSelection () {
            return {
                selectedRowKeys: this.selectedKeys,
                onChange: this.onSelectChange,
            }
        }
    },
    mounted () {
        this.handleDynamicData();
    },
    methods: {
        conversionOrderName,
        //获取动态字段
        handleDynamicData () {
            let { excludeHeadOrFoodColumn } = this;
            let data = {
                source: this.sourceCode
            }
            listDesign(data).then((response) => {
                if (response.code === 200) {
                    const { data } = response;
                    this.dynamicFormDataList = data;
                    if (excludeHeadOrFoodColumn) {
                        this.dynamicFormDataList = data.filter(item => !((item.dataTab == '表头' || item.dataTab == '表尾') && excludeHeadOrFoodColumn.indexOf(item.code) > -1));
                    }
                    if (this.dynamicFormDataList) {
                        this.handleSearchCloum();
                        this.handleSearchArray();
                    }

                    if (this.searchCloumsDetails != []) {
                        //列表加载完成在搜索
                        this.searchList();
                    }

                } else {
                    // // this.$message.warning(res.message);
                }
            })
        },
        //处理搜索数据
        handleSearchArray () {
            const { dynamicFormDataList } = this;
            let data = [];
            for (let i = 0; i < dynamicFormDataList.length; i++) {
                if ((dynamicFormDataList[i].dataTab == '表头' || dynamicFormDataList[i].dataTab == '表尾') && dynamicFormDataList[i].isSearch) {
                    data.push(dynamicFormDataList[i]);
                }
            }
            if (data.length > 0) {
                // this.searchPlaceholder = data.map((o) => o.name).join("/");
                this.searchArray = Object.assign([], data);
                this.$set(this.searchArray, "value", '');
                console.log('searchArray', this.searchArray)
            } else {
                this.$message.warning('没有可以搜索的条件');
            }
        },
        //动态searchCloums组装
        handleSearchCloum () {
            const { dynamicFormDataList } = this;
            let searchCloums = [];

            let dynamicFormAllData = []

            let heade = dynamicFormDataList.filter(item => {
                return item.dataTab == '表头' && (item.onOff == true || item.code == 'voucherState');
            });

            let foote = dynamicFormDataList.filter(item => {
                return item.dataTab == '表尾' && item.onOff == true;
            });

            dynamicFormAllData = Object.assign([], heade)

            dynamicFormAllData.forEach(element => {
                let code = element.code;
                if (element.columnType == '搜索选择') {
                    code = IdFieldToNameField(element.code)
                }

                let item = {
                    title: element.name,
                    dataIndex: code,
                    width: element.columnWidth,
                    columnType: element.columnType,
                    ellipsis: true,
                    sorter: true
                }

                if (element.code == 'businessType') {
                    this.$set(item, 'scopedSlots', { customRender: 'businessType' });
                } else if (element.code == 'voucherCode') {
                    this.$set(item, 'scopedSlots', { customRender: 'voucherCode' });
                } else if (element.code == 'voucherState') {
                    this.$set(item, 'scopedSlots', { customRender: 'voucherState' });
                } else if (element.code == 'workType') {
                    this.$set(item, 'scopedSlots', { customRender: 'workType' });
                } else if (element.code == 'voucherType') {
                    this.$set(item, 'scopedSlots', { customRender: 'voucherType' });
                } else if (element.code == 'planDirection') {
                    this.$set(item, 'scopedSlots', { customRender: 'planDirection' });
                }
                searchCloums.push(item)
            });
            console.log('tagsearchCloums', searchCloums)
            Object.assign(this.searchCloums, searchCloums);
        },
        handleChange (pagination, filters, sorter) {
            console.log('Various parameters', pagination, filters, sorter);

            this.sorterData = {
                page: {
                    current: pagination.current,
                    size: pagination.pageSize
                },
                sortField: sorter ? sorter.field : null,
                sortOrder: sorter ? sorter.order : null,
            }

            this.searchList();
        },
        //改变分页大小
        changePageSize (e) {
            const value = e.target.value;
            let size = Number.parseInt(value);
            if (!size) {
                size = 20;
            }
            if (size > 500) {
                size = 500;
            }

            this.pagination.current = 1;
            this.pagination.pageSize = size;
            this.handleChange({
                current: 1,
                pageSize: size
            });
        },
        searchList (type) {
            //搜索

            let { dynamicFormDataList, searchArray, basePostData } = this;
            if (type == 'all') {
                this.planActive = 0;

                this.searchArray.forEach(item => {
                    item.value = null;
                    item.showvalue = null;
                });
            }

            if (dynamicFormDataList.length == 0 || dynamicFormDataList.length == null) {
                return;
            }
            this.searchData = {
                designSearchDtos: [],
                page: {
                    current: this.pagination.current,
                    size: this.pagination.pageSize,
                },
                // someBusinessType: '0',    // 指定业务类型 默认 0
                isSearchDetail: false,  //  是否查询明细 默认 false
            }
            Object.assign(this.searchData, basePostData);
            Object.assign(this.searchData, this.sorterData);

            for (let i = 0; i < dynamicFormDataList.length; i++) {
                for (let j = 0; j < searchArray.length; j++) {
                    if (dynamicFormDataList[i].code == searchArray[j].code) {
                        Object.assign(dynamicFormDataList[i], searchArray[j])
                    }
                }
            }

            this.searchData.designSearchDtos = dynamicFormDataList;
            if (type == 'myplan' || type == 'all') {
                this.searchData.page.current = this.pagination.current;
                this.searchData.page.size = this.pagination.pageSize;

                // this.pagination.current = 1;
                // this.pagination.pageSize = 10;
            }
            listSearch(this.searchData).then((response) => {
                if (response.code === 200) {
                    let backdData = response.data.records;
                    console.log('--------搜索返回值：', backdData);
                    this.searchBackData = backdData;
                    this.searchBackData.forEach(element => {
                        //处理自定义表头字段的显示
                        if (element.defineFields && element.defineFields != "{}") {
                            let data = JSON.parse(element.defineFields);
                            for (const key in data) {
                                element[key] = data[key];
                            }
                        }
                        element.sourceVoucherType = this.conversionOrderName(element.sourceVoucherType);

                        if(element.outboundStatus == 1){
                            element.outboundStatusId = element.outboundStatus
                            element.outboundStatus = "未发货"
                        } else if(element.outboundStatus == 2){
                            element.outboundStatusId = element.outboundStatus
                            element.outboundStatus = "部分发货"
                        } else if(element.outboundStatus == 3){
                            element.outboundStatusId = element.outboundStatus
                            element.outboundStatus = "全部发货"
                        }

                        if(element.paymentStatus == 1){
                            element.paymentStatusId = element.paymentStatus
                            element.paymentStatus = "未收款"
                        } else if(element.paymentStatus == 2){
                            element.paymentStatusId = element.paymentStatus
                            element.paymentStatus = "部分收款"
                        } else if(element.paymentStatus == 3){
                            element.paymentStatusId = element.paymentStatus
                            element.paymentStatus = "全部收款"
                        }

                        if(element.executionStatus == 1){
                            element.executionStatusId = element.executionStatus
                            element.executionStatus = "未执行"
                        } else if(element.executionStatus == 2){
                            element.executionStatusId = element.executionStatus
                            element.executionStatus = "已计划"
                        } else if(element.executionStatus == 3){
                            element.executionStatusId = element.executionStatus
                            element.executionStatus = "生产中"
                        } else if(element.executionStatus == 4){
                            element.executionStatusId = element.executionStatus
                            element.executionStatus = "已完工"
                        }

                        if(element.releaseState == 1){
                            element.releaseStateId = element.releaseState
                            element.releaseState = "未生单"
                        } else if(element.releaseState == 2){
                            element.releaseStateId = element.releaseState
                            element.releaseState = "已生单"
                        }

                        if(element.closedState == 0){
                            element.closedStateId = element.closedState
                            element.closedState = "关闭"
                        } else if(element.closedState == 1){
                            element.closedStateId = element.closedState
                            element.closedState = "打开"
                        }
                        

                    });

                    this.pagination.total = response.data.total;

                    this.mouseIndex = null;
                } else {
                    // // this.$message.warning(res.message);
                }
            })

            if (type == 'close') {
                this.cancel();
            }
        },
        formatSelectJsonVal (val) {
            let thisVal = JSON.parse(val);
            let backVal = [];
            if (thisVal && thisVal != '' && thisVal != null) {
                thisVal.forEach((item, index) => {
                    backVal.push(item);
                });
            }
            return backVal;
        },
        handleBusinessType (record) {
            let back = '';
            this.dynamicFormDataList.forEach(item => {
                //处理业务类型的展示
                let val = [];
                if (item.code == 'businessType') {
                    val = JSON.parse(item.val);

                    let a = val.filter(c => { return c.key == record.businessType });
                    if (a.length > 0) {
                        back = a[0].label;
                    }
                }
            });
            return back;
        },
        rowClassName (record, index) {
            return this.mouseIndex == index ? 'hoverRowStyl' : '';
        },
        searchGroup (type) {
            //搜索组件的显示
            this.$nextTick(() => {
                this.$refs[`${type}`][0].showModal();
            })

        },
        searchCheckData (record) {
            console.log(record);
            // this.$refs.crmRef.closeSearch();

            this.searchArray.forEach(item => {
                if (item.code == record.codesName) {
                    this.$set(item, 'value', record.id);
                    this.$set(item, 'showvalue', record.name);
                }
            });
            this.$forceUpdate();
        },
        onChangeTime (d, string, code) {
            //时间处理string
            this.searchArray.forEach(item => {
                if (item.code == code) {
                    this.$set(item, 'value', string);
                }
            });
        },
        //处理 下拉选择
        onChangeSelect (code, value) {
            console.log("list查询：下拉选择", code, value);
            this.searchArray.forEach(item => {
                if (item.code == code) {
                    this.$set(item, 'value', value);
                }
            });
            this.$forceUpdate();
        },
        onSelectChange (keys, row) {
            this.selectedKeys = keys;
            this.selectedRows = row;
            console.log(keys)
        },


        //动态查询搜索条件
        searchModelShow () {
            this.visible = true;
        },
        cancel () {
            this.visible = false;
        },
        //获取个人方案列表
        getMyselfPlan () {
            myplan({ reportCode: this.reportCode }).then((res) => {
                if (res.code == 200) {
                    this.myPlanList = res.data;
                }
            }).catch((e) => {
                this.$message.warning('获取个人方案列表_请求出错', 1)
            })
            console.log("获取个人方案列表：", this.myPlanList)
        },
        //点击自定方案事件
        changePlane (data) {

            this.nowPlanData = data;
            let planjson = JSON.parse(data.programmeJson);
            this.dynamicFormDataList.forEach(a => {
                planjson.forEach(b => {
                    if (a.code == b.code) {
                        a.value = b.value;
                        a.showvalue = b.showvalue;
                    }
                });
            });
            this.planActive = data.id;
            this.searchList('myplan');
            // console.log('当前搜索的数据', data.programmeJson)
        },
        //保存方案
        savePlan (t) {
            let data = this.nowPlanData;
            let requestData = {
                id: data.id,
                programmeName: data.programmeName,//方案名称
                programmeJson: JSON.stringify(this.searchArray),   //方案json
                reportCode: this.reportCode,
            }


            if (Number(this.beginCode) > Number(this.endCode)) {
                this.$message.warning('物料编码范围结束值不能小于开始值', 1);
                return;
            }


            console.log('保存的搜索数据：', this.searchArray);
            save(requestData).then((res) => {
                if (res.code == 200) {
                    this.getMyselfPlan();

                    //刷新父组件点击的某个自定义查询
                    this.nowPlanData.programmeJson = requestData.programmeJson;

                    if (t != 'close') {
                        this.$message.success('保存成功', 1);
                    }
                    if (t == 'close') {
                        this.cancel();
                        this.searchList();
                    }
                }
            }).catch((e) => {
                // this.$message.warning('请求出错', 1)
            })
        },
        //保存并查询
        handlerSearch () {
            //自定义方案
            this.savePlan('close');//先保存，在去搜索
        },
        //另存方案
        saveAsPlanShow (type) {
            this.nowPlanType = type;
            this.saveASVisible = true;
            this.saveAsName = type == 2 ? this.nowPlanData.programmeName : '';
        },
        //取消另存方案
        saveAsPlanClose () {
            this.saveASVisible = false;
        },
        //确定另存方案
        saveAsPlanSubmit () { //type==1新增，type==2改名
            let type = this.nowPlanType;
            let name = this.saveAsName;
            if (name == null || name == '') {
                this.$message.warning('请输入查询方案名称', 1);
                return;
            } else {
                let requestData = {
                    id: null,
                    programmeName: name,//方案名称
                    programmeJson: JSON.stringify(this.searchArray),   //方案json
                    reportCode: this.reportCode,
                }

                if (type == 2) {
                    requestData.id = this.planActive
                }

                save(requestData).then((res) => {
                    if (res.code == 200) {
                        if (type == 2) {
                            this.$message.success('自定义方案名称修改成功', 1);
                        } else {
                            this.$message.success('自定义新方案保存成功', 1)

                        }
                        this.getMyselfPlan();
                        this.$emit('newPlan');//刷新父组件数据

                        this.saveASVisible = false;
                        this.saveAsName = '';
                    }
                }).catch((e) => {
                    // this.$message.warning('请求出错', 1)
                })
            }
        },
        //删除方案
        removePlan () {
            let _this = this;
            this.$confirm({
                title: "提示",
                content: (
                    <span>
                        是否确认删除{_this.nowPlanData.programmeName}?
                    </span>
                ),
                onOk () {
                    remove({ id: _this.nowPlanData.id }).then((response) => {
                        if (response.code === 200) {
                            _this.$message.success('删除成功');

                            //如果删除的是默认进入的方案
                            let planchecked = localStorage.getItem(_this.reportCode);
                            console.log(Number(planchecked) == _this.planActive)
                            if (Number(planchecked) == _this.planActive) {
                                localStorage.setItem(_this.reportCode, 0);
                            }

                            _this.getMyselfPlan();

                        } else {
                            // // _this.$message.warning(response.message);
                        }
                    }).catch((e) => {
                        // _this.$message.warning('请求出错', 1)
                    })
                },
                onCancel () { },
            });
        },
    }
}
</script>
<style lang="less">
    .dynamic-list-wrapper .ant-drawer-body {
        padding-bottom: 80px;
    }
    .dynamic-list-wrapper .ant-table-body {
        cursor: pointer;
    }
    .dynamic-list-wrapper .ant-drawer-content {
        z-index: 25;
    }
    .dynamic-list-wrapper .details-title {
        font-weight: bold;
    }
    .dynamic-list-wrapper .hoverRowStyl {
        background-color: rgb(230, 247, 255);
    }
    .order-search-list-box {
        .ant-form-item {
            margin-bottom: 0px;
        }
        label:not(.form-check-label):not(.custom-file-label) {
            font-weight: normal;
        }
    }
    .search-btn-box {
        display: flex;
        border-bottom: 2px solid #1e9fff;
        margin-top: 4px;
        .item-big {
            flex: 0 0 60px;
            font-size: 14px;
            text-align: center;
            color: #000;
            // font-weight: bold;
            line-height: 20px;
            cursor: pointer;
            .icon-searh {
                font-size: 19px;
                color: rgb(45, 144, 236);
            }
        }
        .item-small {
            ul {
                width: 100%;
                list-style: none;
                padding: 0;
                margin: 0;
                clear: both;
            }
            ul > li {
                float: left;
                // border: 1px solid rgb(162, 187, 203);
                color: #333;
                height: 32px;
                line-height: 32px;
                padding: 0px 10px;
                font-size: 14px;
                margin-right: 5px;
                max-width: 120px;
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
            }
            ul > li.active {
                // border: 1px solid rgb(56, 99, 255);
                background: #1e9fff;
                color: #ffffff;
            }
            .icon-searh-btn {
                color: #1e9fff;
            }

            ul > li:hover {
                // border: 1px solid rgb(56, 99, 255);
                background: #1e9fff;
                color: #ffffff;
                .icon-searh-btn {
                    color: #ffffff;
                }
            }
            .item-plan {
                cursor: pointer;
            }
        }
    }
</style>